#!/bin/bash

NACOS="http://localhost:8848"

echo ""
echo "═══════════════════════════════════════════════════════"
echo "✅ Nacos 服务运行演示"
echo "═══════════════════════════════════════════════════════"
echo ""
echo "📊 服务状态:"
echo "  • Nacos 容器: $(docker ps | grep nacos-standalone | awk '{print $NF}')"
echo "  • 访问地址: http://localhost:8848/nacos"
echo ""

echo "─────────────────────────────────────────────────────────"
echo "🔧 演示 1: 配置管理"
echo "─────────────────────────────────────────────────────────"
echo ""
echo "📝 发布配置..."
curl -s -X POST "${NACOS}/nacos/v1/cs/configs" \
  -d "dataId=demo.yaml" \
  -d "group=DEFAULT_GROUP" \
  -d "type=yaml" \
  -d "content=app:
  name: Demo Application
  version: 1.0.0
  port: 8080"

echo ""
echo ""

echo "📖 读取配置..."
CONFIG=$(curl -s -X GET "${NACOS}/nacos/v1/cs/configs?dataId=demo.yaml&group=DEFAULT_GROUP")
echo "配置内容:"
echo "$CONFIG"
echo ""

echo "─────────────────────────────────────────────────────────"
echo "🔍 演示 2: 服务发现"
echo "─────────────────────────────────────────────────────────"
echo ""
echo "📝 注册服务: user-service"
curl -s -X POST "${NACOS}/nacos/v1/ns/instance" \
  -d "serviceName=user-service" \
  -d "ip=192.168.1.10" \
  -d "port=8081" \
  -d "weight=1.5" > /dev/null
echo "   ✅ 已注册"

echo "📝 注册服务: order-service"
curl -s -X POST "${NACOS}/nacos/v1/ns/instance" \
  -d "serviceName=order-service" \
  -d "ip=192.168.1.11" \
  -d "port=8082" \
  -d "weight=1.0" > /dev/null
echo "   ✅ 已注册"

echo "📝 注册多个实例: product-service"
curl -s -X POST "${NACOS}/nacos/v1/ns/instance" \
  -d "serviceName=product-service" \
  -d "ip=192.168.1.20" \
  -d "port=8083" \
  -d "weight=2.0" > /dev/null
curl -s -X POST "${NACOS}/nacos/v1/ns/instance" \
  -d "serviceName=product-service" \
  -d "ip=192.168.1.21" \
  -d "port=8084" \
  -d "weight=1.0" > /dev/null
echo "   ✅ 已注册 2 个实例"
echo ""

sleep 2

echo "📋 查询所有服务..."
SERVICES=$(curl -s -X GET "${NACOS}/nacos/v1/ns/service/list?pageNo=1&pageSize=100")
echo "$SERVICES" | python3 -m json.tool 2>/dev/null || echo "$SERVICES"
echo ""

echo "🔎 查询 product-service 实例列表..."
curl -s -X GET "${NACOS}/nacos/v1/ns/instance/list?serviceName=product-service" | python3 -m json.tool | head -30
echo ""

echo "─────────────────────────────────────────────────────────"
echo "🧹 清理测试数据"
echo "─────────────────────────────────────────────────────────"
curl -s -X DELETE "${NACOS}/nacos/v1/ns/instance" -d "serviceName=user-service" -d "ip=192.168.1.10" -d "port=8081" > /dev/null
curl -s -X DELETE "${NACOS}/nacos/v1/ns/instance" -d "serviceName=order-service" -d "ip=192.168.1.11" -d "port=8082" > /dev/null  
curl -s -X DELETE "${NACOS}/nacos/v1/ns/instance" -d "serviceName=product-service" -d "ip=192.168.1.20" -d "port=8083" > /dev/null
curl -s -X DELETE "${NACOS}/nacos/v1/ns/instance" -d "serviceName=product-service" -d "ip=192.168.1.21" -d "port=8084" > /dev/null
curl -s -X DELETE "${NACOS}/nacos/v1/cs/configs?dataId=demo.yaml&group=DEFAULT_GROUP" > /dev/null
echo "✅ 清理完成"
echo ""

echo "═══════════════════════════════════════════════════════"
echo "✅ Nacos 演示完成！"
echo "═══════════════════════════════════════════════════════"
echo ""
echo "🎯 下一步："
echo "  1. 访问 Nacos 控制台: http://localhost:8848/nacos"
echo "  2. 查看服务列表和配置列表"
echo "  3. 尝试手动创建配置"
echo "  4. 运行其他代码示例"
echo ""
echo "📚 可运行的示例："
echo "  • ./scripts/nacos-cli.sh help"
echo "  • examples/python-client/（Python 示例）"
echo "  • examples/java-config/（Java 示例）"
echo ""
